﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TransformCost.Question;

namespace TransformCost.Answer
{
    class Libby : ITransformCost
    {
        public int GetMinExchanges(int[] source, int[] target)
        {
            int[] count = new int[source.Length];
            for (int i = 0; i < source.Length;i++ )
            {
                int index=0;
                for(int j=0;j<source.Length;j++)
                {
                    if(target[j]==source[i])
                    {
                        index=j;
                    }
                }
                count[i] = index - i;
            }

            int leastExchange = 0;
            int start = 0;
            int last = 0;
            for (int i = 0; i < count.Length;i++ )
            {
                if(count[i]>0)
                {
                    start = i;
                    break;
                }
            }

            for (int i = count.Length-1; i>=0; i--)
            {
                if (count[i] > 0)
                {
                    last = i;
                    break;
                }
            }

            for (int i = start; i <= last;i++ )
            {
                if (count[i] == 0)
                {
                    leastExchange++;
                }
                else
                    leastExchange += count[i];
            }

            return leastExchange;
        }
    }
}
